package com.yanqu.road.dao.impl.activity.drink;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.drink.DrinkOrder;
import com.yanqu.road.entity.activity.drink.DrinkUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class DrinkUserDataDaoImpl extends TempDataObjectDao<DrinkUserData> {

    public static String getDrinkUserDataSql = "select * from `t_u_activity_drink_user_data` where `activityId`=? and `userId`=?";

    public DrinkUserData getDrinkUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getDrinkUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateDrinkUserDataSql = "update `t_u_activity_drink_user_data` set score=?,energy=?,lastRecTime=?,guideIndex=?,blockList1=?,blockList4=?,blockList16=?,order1=?,order4=?,order16=?,waitRewardOrderList1=?,waitRewardOrderList4=?,waitRewardOrderList16=?,orderIdList1=?,orderIdList4=?,orderIdList16=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(DrinkUserData drinkUserData) {
        drinkUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, drinkUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, drinkUserData.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, drinkUserData.getLastRecTime()));
        para.put(i++, new DbParameter(Types.INTEGER, drinkUserData.getGuideIndex()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drinkUserData.getBlockList1())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drinkUserData.getBlockList4())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drinkUserData.getBlockList16())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drinkUserData.getOrder1())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drinkUserData.getOrder4())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drinkUserData.getOrder16())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drinkUserData.getWaitRewardOrderList1())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drinkUserData.getWaitRewardOrderList4())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drinkUserData.getWaitRewardOrderList16())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drinkUserData.getOrderIdList1())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drinkUserData.getOrderIdList4())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drinkUserData.getOrderIdList16())));
        para.put(i++, new DbParameter(Types.INTEGER, drinkUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, drinkUserData.getUserId()));
        if (getDb().executeNoneQuery(updateDrinkUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertDrinkUserDataSql = "insert into `t_u_activity_drink_user_data`(activityId,userId,score,energy,lastRecTime,guideIndex,blockList1,blockList4,blockList16,order1,order4,order16,waitRewardOrderList1,waitRewardOrderList4,waitRewardOrderList16,orderIdList1,orderIdList4,orderIdList16) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(DrinkUserData drinkUserData) {
        drinkUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, drinkUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, drinkUserData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, drinkUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, drinkUserData.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, drinkUserData.getLastRecTime()));
        para.put(i++, new DbParameter(Types.INTEGER, drinkUserData.getGuideIndex()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drinkUserData.getBlockList1())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drinkUserData.getBlockList4())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drinkUserData.getBlockList16())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drinkUserData.getOrder1())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drinkUserData.getOrder4())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drinkUserData.getOrder16())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drinkUserData.getWaitRewardOrderList1())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drinkUserData.getWaitRewardOrderList4())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drinkUserData.getWaitRewardOrderList16())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drinkUserData.getOrderIdList1())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drinkUserData.getOrderIdList4())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drinkUserData.getOrderIdList16())));
        if (getDb().executeNoneQuery(insertDrinkUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected DrinkUserData parseData(ResultSet rs) throws SQLException {
        DrinkUserData drinkUserData = new DrinkUserData();
        drinkUserData.setActivityId(rs.getInt("activityId"));
        drinkUserData.setUserId(rs.getLong("userId"));
        drinkUserData.setScore(rs.getLong("score"));
        drinkUserData.setEnergy(rs.getInt("energy"));
        drinkUserData.setLastRecTime(rs.getLong("lastRecTime"));
        drinkUserData.setGuideIndex(rs.getInt("guideIndex"));
        drinkUserData.setBlockList1(JSON.parseObject(rs.getString("blockList1"), int[][].class));
        drinkUserData.setBlockList4(JSON.parseObject(rs.getString("blockList4"), int[][].class));
        drinkUserData.setBlockList16(JSON.parseObject(rs.getString("blockList16"), int[][].class));
        drinkUserData.setOrder1(JSON.parseObject(rs.getString("order1"), DrinkOrder.class));
        drinkUserData.setOrder4(JSON.parseObject(rs.getString("order4"), DrinkOrder.class));
        drinkUserData.setOrder16(JSON.parseObject(rs.getString("order16"), DrinkOrder.class));
        drinkUserData.setWaitRewardOrderList1(JSON.parseObject(rs.getString("waitRewardOrderList1"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.drink.DrinkOrder>>() {
        }));
        drinkUserData.setWaitRewardOrderList4(JSON.parseObject(rs.getString("waitRewardOrderList4"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.drink.DrinkOrder>>() {
        }));
        drinkUserData.setWaitRewardOrderList16(JSON.parseObject(rs.getString("waitRewardOrderList16"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.drink.DrinkOrder>>() {
        }));
        drinkUserData.setOrderIdList1(JSON.parseObject(rs.getString("orderIdList1"), new TypeReference<java.util.List<java.lang.Integer>>() {
        }));
        drinkUserData.setOrderIdList4(JSON.parseObject(rs.getString("orderIdList4"), new TypeReference<java.util.List<java.lang.Integer>>() {
        }));
        drinkUserData.setOrderIdList16(JSON.parseObject(rs.getString("orderIdList16"), new TypeReference<java.util.List<java.lang.Integer>>() {
        }));
        return drinkUserData;
    }

}